/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package simulation;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

/**
 *
 * @author haoliang.donghl
 */
public class Application {
    public static ArrayList<Application> allApps = new ArrayList<Application>();
    protected static int LOAD_NUM = 72;
    protected float load;
    protected Instance[] instances;
    protected float[] loads;
    protected int id;
    protected String name;
    java.awt.Color color;

    public void readLoadsFromFile(String filename){
        try {
            Scanner scanner = new Scanner(new File(filename));
            loads = new float[LOAD_NUM];
            int p = 0;
            while(scanner.hasNextFloat()){
                loads[p] = scanner.nextFloat();
                p++;
            }
            scanner.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public void updateLoad(float point){
        float t = point * (loads.length - 1);
        int p1 = (int) Math.floor(t);
        int p2 = (int) Math.ceil(t);
        load = loads[p1] * (1 - t + p1) + loads[p2] * (1 - p2 + t);
        load *= 0.000025;
        for (Instance instance: instances){
            instance.updateLoad();
        }
    }

    public static Application getAppById(int id){
        for (Application app : allApps){
            if (app.getId() == id) return app;
        }
        return null;
    }

    public static Application getAppByName(String name){
        for (Application app : allApps){
            if (name.equals(app.getName())) return app;
        }
        return null;
    }

    public static int getAppNum(){
        return allApps.size();
    }

    public Application(String name){
        this.load = 0;
        this.instances = new Instance[Machine.getMachineNum()];
        for (int i = 0; i < Machine.getMachineNum(); i++){
            Machine machine = Machine.getMachineById(i + 1);
            instances[i] = new Instance(this, machine);
        }
        this.name = name;
        allApps.add(this);
        id = allApps.size();
        color = new java.awt.Color((int)(Math.random() * 255),
                                   (int)(Math.random() * 255),
                                   (int)(Math.random() * 255));
    }

    public int getId(){
        return id;
    }

    public void setLoad(float load){
        this.load = load;
    }

    public float getLoad(){
        return load;
    }

    public String getName(){
        return name;
    }

    public Instance[] getInstances(){
        return instances;
    }

    public Instance getInstance(Machine machine){
        return instances[machine.getId() - 1];
    }

    public Instance getInstance(int id){
        return instances[id - 1];
    }

    public int getRunningNum(){
        int c = 0;
        for (int i = 0; i < instances.length; i++){
            if (instances[i].getState() == Instance.STATE_RUNNING)
                c++;
        }
        return c;
    }
}
